package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.ReplyListBean;
import com.warmlight.voicepacket.ui.user.usercenter.LookUserCenterActivity;
import com.warmlight.voicepacket.widget.ReportCopaPopWindow;

import java.util.ArrayList;
import java.util.List;

public class CommentDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public interface OnZanDataListener {
        void zanData(String id, int Postion, boolean iszan);
    }

    public OnZanDataListener onZanDataListener;

    public void setOnZanDataListener(OnZanDataListener listener) {
        onZanDataListener = listener;
    }

    private List<ReplyListBean.ListBean> list;
    private  ReplyListBean.ReplyInfoBean bean;
    private Context context;
    private final RequestOptions options;

    /**
     * @param list    评论列表
     * @param context 上下文
     */
    public CommentDetailAdapter(List<ReplyListBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
        options = new RequestOptions();
        options.placeholder(R.drawable.icon_login_header).error(R.drawable.icon_login_header).dontAnimate();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
            return new CommentDetailAdapter.ViewHolders(view);


    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {  //根据你的条件，返回不同的type
            return 0;
        } else {
            return 1;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final ViewHolders holderOnes = (ViewHolders) holder;
        final  ReplyListBean.ListBean bean = list.get(position);
        holderOnes.iv_zan.setOnClickListener(new View.OnClickListener() {//点赞数 点赞 加减赞
            @Override
            public void onClick(View view) {
                onZanDataListener.zanData(bean.getId(), position, bean.isIs_like()==1);
            }
        });
        if (getItemViewType(position) == 0) {
            holderOnes.blodlin.setVisibility(View.VISIBLE);
        }else {
            holderOnes.blodlin.setVisibility(View.GONE);
        }
        holderOnes.ll_commentback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        holderOnes.rl_comment.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (position!=0) {
                    ReportCopaPopWindow popWindow = new ReportCopaPopWindow(context);
                    popWindow.setID(bean.getId(), bean.getContent());
                    popWindow.showPopupWindow(holderOnes.iv_zan);

                }
                return false;
            }
        });
            if (bean.isIs_like()==1) {//设置点赞的状态

                holderOnes.iv_zan.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_community_zan_true));
            } else {
                holderOnes.iv_zan.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_community_zan_false));
            }


            holderOnes.tv_time.setText(bean.getDate());
            holderOnes.tv_contentcomment.setText(bean.getContent());
            holderOnes.tv_authortitle.setText(bean.getUser().getNickname());
            String priaise = bean.getLike_num();
//            String priaise = FormUnitUtils.form(BigDecimal.valueOf(bean.getLike_num()), "w");
            if (TextUtils.equals("0",priaise)){
                priaise = "点赞";
            }
            holderOnes.tv_zannum.setText(priaise);
            holderOnes.tv_replynum.setText(bean.getReply_num());
                holderOnes.iv_comententry.setVisibility(View.GONE);
                holderOnes.tv_replynum.setVisibility(View.GONE);
            if (TextUtils.equals("1", bean.getIs_god())) {
                holderOnes.iv_glodcomment.setVisibility(View.VISIBLE);
            }else{
                holderOnes.iv_glodcomment.setVisibility(View.GONE);
            }
            if (options != null) {
                Glide.with(context).asBitmap().load(bean.getUser().getAvator()).apply(options).into(holderOnes.iv_authoeriv);
            }

            holderOnes.iv_authoeriv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LookUserCenterActivity.class);
                    intent.putExtra("uid",bean.getUser().getUid());
                    intent.putExtra("type","video");
                    context.startActivity(intent);
                }
            });

        holderOnes.tv_authortitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LookUserCenterActivity.class);
                intent.putExtra("uid",bean.getUser().getUid());
                intent.putExtra("type","video");
                context.startActivity(intent);
            }
        });

            ArrayList<ImageInfo> imageInfo = new ArrayList<>();
            for (int i = 0; i < bean.getPics().size(); i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(bean.getPics().get(i).getU());
                info.setBigImageUrl(bean.getPics().get(i).getU());
                imageInfo.add(info);
            }
        holderOnes.nineGrid.setSingleImageSize(400);
        holderOnes.nineGrid.setAdapter(new NineGridViewClickAdapter(context, imageInfo));


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolders extends RecyclerView.ViewHolder {
        NineGridView nineGrid;
        TextView tv_authortitle, tv_time, tv_contentcomment, tv_zannum, tv_replynum;
        LinearLayout ll_commentback;
        ImageView iv_authoeriv;
        ImageView iv_glodcomment;
        ImageView iv_comententry;
        ImageView iv_zan;
        RelativeLayout rl_comment;
        View blodlin;


        ViewHolders(View itemView) {
            super(itemView);
            nineGrid = itemView.findViewById(R.id.nineGrid);
            tv_authortitle = itemView.findViewById(R.id.tv_authortitle);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_glodcomment = itemView.findViewById(R.id.iv_glodcomment);
            tv_contentcomment = itemView.findViewById(R.id.tv_contentcomment);
            iv_authoeriv = itemView.findViewById(R.id.iv_authoeriv);
            tv_zannum = itemView.findViewById(R.id.tv_zannum);
            ll_commentback = itemView.findViewById(R.id.ll_commentback);
            iv_zan = itemView.findViewById(R.id.iv_zan);
            tv_replynum = itemView.findViewById(R.id.tv_replynum);
            iv_comententry = itemView.findViewById(R.id.iv_comententry);
            blodlin = itemView.findViewById(R.id.blodlin);
            rl_comment=itemView.findViewById(R.id.rl_comment);

        }
    }

}